<?php

namespace app\models;

use app\components\BaseActiveRecord;
use app\helpers\ArrayHelper;
use app\utils\ArrayUtils;
use app\utils\BaseUtil;
use Yii;
use yii\helpers\Html;

/**
 * This is the model class for table "{{%admin_menu}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $icon
 * @property string $name
 * @property string $url
 * @property integer $status
 * @property integer $sort
 * @property integer $ctime
 */
class AdminMenu extends BaseActiveRecord {

    public $pageSize = FALSE;
    public $sortAttributes = ['sort'];
    public $defaultOrder = ['sort' => SORT_ASC];

    const STATUS_HIDE = 1;
    const STATUS_NORMAL = 0;

    public function init() {
        parent::init();
        $this->sort = $this->parent_id = 0;
    }

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%admin_menu}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['status'], 'in', 'range' => [self::STATUS_HIDE, self::STATUS_NORMAL]],
            [['parent_id', 'status', 'sort', 'ctime'], 'integer'],
            [['icon'], 'string', 'max' => 50],
            [['name'], 'string', 'max' => 100],
            [['url'], 'string', 'max' => 60],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '父级',
            'icon' => 'Icon',
            'name' => '名称',
            'url' => 'Url',
            'status' => '状态',
            'sort' => '排序',
            'ctime' => 'Ctime',
        ];
    }

    public static function getStatusNames() {
        return array(
            self::STATUS_HIDE => '隐藏',
            self::STATUS_NORMAL => '显示',
        );
    }

    public function getIsHide() {
        return intval($this->status) === intval(self::STATUS_HIDE);
    }

    public function getStatusName() {
        $names = self::getStatusNames();
        $name = $names[$this->status];
        if ($this->getIsHide()) {
            return Html::tag('b', $name, ['class' => 'text-maroon']);
        }
        return Html::tag('i', $name);
    }

    public function access() {
        return static::find()->where(['status' => self::STATUS_NORMAL])
            ->orderBy('sort asc, id desc')
            ->asArray()
            ->indexBy('id')
            ->all();
    }

    public function getUserMenus() {
        if (Admin::loginUserIsRoot()) {
            $menus = $this->access();
        } else {
            /* @var $admin \app\models\Admin */
            $admin = Yii::$app->user->identity;
            if (!$admin->group || empty($admin->group->privilege_ids)) {
                $menus = [];
            } else {
                $menuIds = $admin->group->getPrivilegeIds();
                $menus = static::find()->where(['id' => $menuIds, 'status' => self::STATUS_NORMAL])
                    ->orderBy('sort asc, id desc')
                    ->asArray()->indexBy('id')->all();
            }
        }
        return $menus;
    }

    public function formatterMenu() {
        $menus = $this->getUserMenus();

        $arrays = ArrayHelper::arrayTree($menus);
        $data = array();
        foreach ($arrays as $k => $array) {
            $icon = empty($array['icon']) ? '' : "fa-{$array['icon']}";
            $data[$k]['label'] = "<i class=\"fa {$icon}\"></i>{$array['name']}";
            $data[$k]['name'] = $array['name'];
            $data[$k]['url'] = empty($array['url']) ? 'javascript:;' : array($array['url']);
            if (isset($array['child'])) {
                $data[$k]['label'] .= "<i class=\"fa fa-angle-down arrow\"></i>
";
                foreach ($array['child'] as $a => $child) {
                    $data[$k]['items'][$a]['label'] = "<i class=\"ion fa fa-angle-double-right\"></i>{$child['name']}";
                    $data[$k]['items'][$a]['url'] = empty($child['url']) ? '' : array($child['url']);
                }
            }
        }
        return $data;
    }

    public function getCategory() {
        $data = array(0 => '无');
        $menus = static::find()
            ->orderBy('sort asc, id desc')
            ->asArray()
            ->indexBy('id')
            ->all();
        ArrayHelper::arrayDepthSort($menus, $lists);

        if (!$this->isNewRecord) {
            $lists = ArrayHelper::removeChild($lists, $this->id);
        }

        if (empty($lists)) {
            return $data;
        }

        foreach ($lists as $list) {
            $data[$list['id']] = $list['_title'];
        }
        return $data;
    }
}
